<?php
	class roleControl extends Control{
		function index(){
			$db=M("role");
			$count = $db->count();
			$page = new page($count,6);
			$row=$db->limit($page->limit())->all();
			if (!$row) {
				$this->error("没有任何组(角色)信息，请先设置", "add_show");
			}
			$this->assign("row",$row);
			$this->assign("page", $page->show(2));
			$this->display();
	
		}
		
		function add_show(){
			$this->display();
		}
		
		function add(){
			$db=M("role");
			if($db->add()){
				$this->success("添加用户组成功！","index");
			}else{
				$this->error("添加用户组失败！");
			}
		}
		
		function edit_show(){
			$rid=$_GET["rid"];
			$db=M("role");
			$role=$db->where("rid=$rid")->find();
			//p($role);
			$this->assign("role",$role);
			$this->display();
		
		}
		function edit_save(){
			$db=M("role");
			//p($_POST);exit;
			if($db->save()){
				$this->success("修改用户组成功！","index");
			}else{
				$this->error("修改用户组失败！");
			}
		
		}
		function del(){
			$rid=$_GET["rid"];
			$db=M("role");
			if($db->where("rid=$rid")->del()){
				$this->success("删除用户组成功！","index");
			}else{
				$this->error("删除用户组失败！");
			}
		
		
		}
		function setaccess() {
			$rid = $_GET['rid'];
			$db = M("role");
			$role = $db->find($rid);
			$this->assign('role', $role);
			$db=M("node");
			/* $node=$db->all();
			$node = arr::channel($node,"nid","pid",0,2,"&nbsp;&nbsp;"); */
			 $rbac = new rbac();
			$node = $rbac->getNodeList($rid); 
			//p($node); 
			if (!$node) {
				$this->error("还没有设置权限节点，请设置", 'showaddnode');
			}
			$this->assign('node', $node);
			$this->display();
		}
		
		function addaccess() {
        $db = M("access");
        $db->where("rid=" . $_POST['rid'])->del();
        if (empty($_POST['node'])) {
            $this->success("已经全部删除");
        } else {
            foreach ($_POST['node'] as $v) {
                $db->rid = $_POST['rid'];
                $db->nid = $v;
                $db->add($db);
            }
            $this->success("操作成功");
        }
    }
		
	}
?>